package com.parking.carsystem.parkingchargesystem.activity;

import android.os.Bundle;
import android.os.Message;
import android.provider.SyncStateContract;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alipay.sdk.app.EnvUtils;
import com.alipay.sdk.app.PayTask;
import com.alipay.sdk.util.H5PayResultModel;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.AbsenceData;
import com.parking.carsystem.parkingchargesystem.module.BillMessage;
import com.parking.carsystem.parkingchargesystem.module.CouponDescModel;
import com.parking.carsystem.parkingchargesystem.module.PayResult;
import com.parking.carsystem.parkingchargesystem.module.PurchasePayModel;
import com.parking.carsystem.parkingchargesystem.utils.WXPayUtils;
import com.parking.carsystem.parkingchargesystem.view.BottomDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.parking.carsystem.parkingchargesystem.common.Constant.SDK_PAY_FLAG;

public class SaveMoneyRollsActivity extends BaseActivity implements View.OnClickListener, BottomDialog.ChooseTextLisenter {


    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.setting)
    TextView setting;
    @BindView(R.id.coupon_message)
    TextView couponMessage;
    @BindView(R.id.coupon_describe)
    TextView couponDescribe;
    @BindView(R.id.term_of_validity)
    TextView termOfValidity;
    @BindView(R.id.term_of_validity_time)
    TextView termOfValidityTime;
    @BindView(R.id.pay_count)
    TextView payCount;
    @BindView(R.id.reduce)
    TextView reduce;
    @BindView(R.id.add)
    TextView add;
    @BindView(R.id.coupon_desc)
    TextView couponDesc;
    @BindView(R.id.purchase)
    TextView purchase;
    @BindView(R.id.prices)
    TextView prices;
    @BindView(R.id.prices_count)
    LinearLayout pricesCount;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.coupon_count)
    EditText couponCount;
    private int couponId;
    private int count;
    private CouponDescModel couponDescModel;
    private ArrayList<AbsenceData> absenceData;
    private BottomDialog bottomDialog;
    private PurchasePayModel purchasePayModel;

    public void handleMessageMeathed(Message msg) {
        boolean flag = payResponse(msg);
        if(flag){
            finish();
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_save_money_rolls;
    }

    @Override
    public void initView() {
        //支付宝的开启沙箱环境支付
//        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
        couponId = getIntent().getIntExtra(Constant.COUPONID, 0);
        back.setVisibility(View.VISIBLE);
        text.setText("省钱卷包");
        back.setOnClickListener(this);
        add.setOnClickListener(this);
        reduce.setOnClickListener(this);
        purchase.setOnClickListener(this);
        getEditextCount();
    }

    @Override
    public void initData() {
        absenceData = new ArrayList<>();
        //三种地图
        absenceData.add(new AbsenceData("支付宝", false));
        absenceData.add(new AbsenceData("微信", false));
//        absenceData.add(new AbsenceData("账户余额", false));
        bottomDialog = new BottomDialog(absenceData.size(), this);
        bottomDialog.setChooseTextLisenter(this);
        bottomDialog.setBottomData(absenceData);
        getDetailMessage();
    }

    private void getDetailMessage() {
        OkGo.<String>get(Constant.url + Constant.GETDETAIL + couponId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        couponDescModel = gson.fromJson(response.body(), CouponDescModel.class);
                        if (couponDescModel.code == 200) {
                            couponMessage.setText(couponDescModel.data.name);
                            couponDescribe.setText("限购" + couponDescModel.data.singlePurchaseLimit + "张," + "每张" + couponDescModel.data.unitPrice + "元");
                            termOfValidityTime.setText(couponDescModel.data.startTime + " - " + couponDescModel.data.endTime);
                            couponDesc.setText("限" + couponDescModel.data.singlePurchaseLimit + "张之内," + "每张" + couponDescModel.data.unitPrice + "元");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {

                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.add:
                getEditextCount();
                if (count >= couponDescModel.data.singlePurchaseLimit) {
                    showToast("限" + couponDescModel.data.singlePurchaseLimit + "张之内");
                    return;
                }
                couponCount.setText(String.valueOf(++count));
                prices.setText("¥" + count * couponDescModel.data.unitPrice);
                break;
            case R.id.reduce:
                getEditextCount();
                if (count != 0) {
                    couponCount.setText(String.valueOf(--count));
                    prices.setText("¥" + count * couponDescModel.data.unitPrice);
                }
                break;
            case R.id.purchase:
                if (count != 0) {
                    purchasePay();
                }
                break;
        }
    }

    /***
     *
     */
    private void purchasePay() {
        getEditextCount();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("couponId", String.valueOf(couponId));
        hashMap.put("count", String.valueOf(count));
        JSONObject jsonObject = new JSONObject(hashMap);
        OkGo.<String>post(Constant.url + Constant.SELECTPREPAYCOUPONORDER)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        purchasePayModel = gson.fromJson(response.body(), PurchasePayModel.class);
                        if (purchasePayModel.code == 200) {
                            //去支付
//                            showToast("支付功能暂未开发");
                            bottomDialog.show();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {


                    }
                });
    }

    private void getEditextCount() {
        count = Integer.parseInt(couponCount.getText().toString().trim());
    }

    @Override
    public void chooseText(AbsenceData text) {
        if (text.text.equals("微信")) {
            payWeiXin(purchasePayModel.data.outTradeNo);
        } else if(text.text.equals("支付宝")){
            payAli(purchasePayModel.data.outTradeNo);
        } /*else if(text.text.equals("账户余额")){
            accountPay(purchasePayModel.data.outTradeNo);
        }*/
    }
}
